package org.sibutu.plugin.struts2.jcr.wrapper;


import org.sibutu.plugin.struts2.jcr.wrapper.NodeWrapperImpl;
import org.sibutu.plugin.struts2.jcr.wrapper.NodeTextPropRetriever;
import org.sibutu.plugin.struts2.jcr.wrapper.WrapperFactory;

import javax.jcr.*;
import java.util.Map;

/**
 * @author : Max Skripnikov
 */
public class TextPropsWrapper extends NodeWrapperImpl {
  private Map<String,String> propsSubstitutionMap;
  private NodeTextPropRetriever textPropRetriever;

  public TextPropsWrapper(Node node, Map<String,String> propsSubstitutionMap, NodeTextPropRetriever textPropRetriever, WrapperFactory wrapperFactory) {
    super(node,wrapperFactory);
    this.propsSubstitutionMap = propsSubstitutionMap;
    this.textPropRetriever = textPropRetriever;
  }


  public Object get(Object key) {
    if(propsSubstitutionMap.containsKey(key)){
      try {
        return textPropRetriever.getTextProp(this, propsSubstitutionMap.get(key));
      } catch (RepositoryException e) {
        throw new RuntimeException(e);
      }
    }
    return super.get(key);
  }

}
